package com.longcai.piaojia.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.activity.SearchActivity;
import com.longcai.piaojia.adapter.SearchLastListAdapter;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.longcai.piaojia.widget.NoScrollListView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/3.
 */
public class SearchLastFragment extends BaseFragment implements View.OnClickListener{

    @BoundView(R.id.search_last_lv) private NoScrollListView search_last_lv;
    @BoundView(R.id.search_last_clean_ll) private LinearLayout search_last_clean_ll;
    private SearchLastListAdapter searchLastListAdapter;
    private List<String> lists = new ArrayList<>();
    public static boolean is_live = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        is_live = true;

        View view = BoundViewHelper.boundView(this, inflater.inflate(R.layout.fragment_search_last, null));

        search_last_clean_ll.setOnClickListener(this);

        setAppCallBack((AppCallBack) new RefreshSearchNotes());

        searchLastListAdapter = new SearchLastListAdapter(getActivity(), lists);
        search_last_lv.setAdapter(searchLastListAdapter);

        search_last_lv.setDividerHeight(ScaleScreenHelperFactory.getInstance().getWidthHeight(1));

        search_last_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ((SearchActivity) getActivity()).closeKeybord();

                ((SearchActivity) getActivity()).search(lists.get(position));

                //下面代码是 把点击的移动到第一条, 如果list长度少于2  就不刷新

                if (lists.size() < 2) {

                    return;
                }

                List<String> midLists = new ArrayList<String>();

                midLists.addAll(lists);

                lists.clear();

                lists.add(midLists.get(position));

                for (int i = 0; i < midLists.size(); i++) {

                    if (position != i){

                        lists.add(midLists.get(i));
                    }

                }

                searchLastListAdapter.notifyDataSetChanged();

//                startActivity(new Intent(getActivity(), SearchResultActivity.class).putExtra("key", lists.get(position)));
//
//                search(lists.get(position));

            }
        });

        new RefreshSearchNotes().refresh();

        return view;

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.search_last_clean_ll:



                new WarnningDialog(getActivity(), "清空记录") {
                    @Override
                    protected void onConfirm() {

                        ((SearchActivity) getActivity()).closeKeybord();

                        BaseApplication.BasePreferences.saveSearch("");

                        new RefreshSearchNotes().refresh();

                    }
                }.show();


                break;

        }

    }

    public class RefreshSearchNotes implements AppCallBack{

        public void refresh(){

            lists.clear();

            String searchNotes = BaseApplication.BasePreferences.readSearch();

                if (!TextUtils.isEmpty(searchNotes)) {

                    if (searchNotes.contains(",")) {

                        String[] arrStr = searchNotes.split(",");

                        for(int i=0; i < arrStr.length; i++) {

                            lists.add(arrStr[i]);

                        }

                    }else{

                        lists.add(searchNotes);
                    }

                }

            searchLastListAdapter.notifyDataSetChanged();

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        is_live = false;

    }
}
